package com.stores.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.stores.action.ManageManufacturerAction;
import com.stores.model.Manufacturer;
import com.stores.toolset.InputValidator;
import com.stores.toolset.WebAction;

public class ManufacturersController extends HttpServlet {

	private static final String ADMIN_MFG_SAVE = "/admin/mfg/save";
	private static final String ADMIN_MFG_LOAD = "/admin/mfg/load";
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/***
	 * 
	 */
	@SuppressWarnings("static-access")
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		ServletContext context = getServletContext();
		String requestUrl = request.getRequestURL().toString();
		if (requestUrl.indexOf("manage-mfg") > -1) {
			RequestDispatcher dispatcher = context
					.getRequestDispatcher("/admin/manage-mfg.jsp");
			try {
				dispatcher.forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		} else {
			try {
				response.sendError(response.SC_BAD_REQUEST,
						"Valid url is /admin/manage-mfg");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		}
	}

	/***
	 * 
	 */
	@SuppressWarnings("static-access")
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		WebAction webAction = new WebAction(request, response);
		ManageManufacturerAction manage = new ManageManufacturerAction();
		InputValidator validator = new InputValidator();

		if (webAction.isUrl(ADMIN_MFG_LOAD)) {
			Gson gson = new Gson();
			webAction.writeResponse(gson.toJson(manage.Retrive()));
		} else {
			String description = request.getParameter("mfg-desc");
			String name = request.getParameter("mfg-name");

			if (validator.isValid(description + name)) {// club them
				// together
				// and
				// send for
				// validation
				Manufacturer mfg = new Manufacturer();
				mfg.setDescription(description);
				mfg.setName(name);
				mfg.setLargeImage("https://");
				mfg.setMediumImage("https://");
				mfg.setSmallImage("https://");
				if (webAction.isUrl(ADMIN_MFG_SAVE)) {
					String mfgId = request.getParameter("mfg-id");
					if (mfgId.length() == 0) {
						manage.Create(mfg);
					} else {
						mfg.setMfgId(Integer.parseInt(mfgId));
						manage.Update(mfg);
					}
				}
			} else {
				try {
					response.sendError(response.SC_BAD_REQUEST,
							"Invalid characters found in the input data. Data validation failed!!");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					// e.printStackTrace();
				}
			}
		}
	}
}
